package com.zerowidth.moc.web.pay.ali;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.zerowidth.moc.web.utils.LogUtil;

import java.util.Map;

public class AlipayOrderFactory {

    private static String serverUrl = "https://openapi.alipay.com/gateway.do";
    private static String APP_ID = "2021004142649030";
    private static String FORMAT_JSON = "json";
    private static String CHARSET = "utf-8";
    private static String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC+nkn1gNExsIzssNZD7nXHBqv05J3Yut4LpUScbGVaI9nJLazfwMHL+gOCY9uG4lNde9C38gyLYamXaiPu5GyFQ7vOux8mRs/nIECgjzvHliEV/pvbw0jA+mTNdGK3cTXf/Qms4mG+8nV4q3cxLKoe4QgNgMCNJQ+3zukQ09lcQjBqKV5jv2Z0g6z+Ymf+VLNwTgc68bE5VD/a5QypoTyBbmhREP1dd/kdLJtHae+Q9Cd8cYhSlqm1DmgCl7vd7jvAUA85wlEZoBAyr60NlAo6jgwSd/9VPUC7Oqr8d9yyPTfhj3NLQ9CepGG4VMf34faZ1OL69pz8NS08ynJaSZqrAgMBAAECggEAD2LjgWeWezZPCcP0Z6NV6mzFcI0LmLTsu2/pq96sB6yhJbYEIEOE4RwbMGkzDR4zsHYQEOj+zjlh4RD2OZBCmjcvb653AzXwDi3ySYOGRcP+g7H7jq+ircEIZfUhCsGeoafhIg1b6nfTt5vPkS6TxM5RQhXEdSAqtqGC+pK/Fs01HRXWESrGnQRGagJPqfFookJ1kL1gcBYwl9uY0pMWVtJxssZteIkQYcMp14s68zi6vF0+sYFWYkco3SUzYGt9acR2qSdGgsoNxhs2sJ/hHi+jBKqj4XCh4plmupGsbN0NdMfD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ODuRADQAv2laR/5PRYxYyzfe9gmmvJeHwfQOpXAN7u9SWWOewLLqDkC7VBY6ZcmIlq1uxDHtEh6hRcFZQdn7McegHgKsQyozuUi5XXfwgBWA/G+Xjc44ydSxKAMWbxY/PdPSz6sd/WRHPIGRKFcYbWJHJfMhCBIOYzor2o0fQS2r6ytfsiEfczZEiMkgUCKeuiZo6MO7nx4z0FGFsmaSNQglEfqLKwT5IqAogNIqf8+rOSch8CsaCNW1oc/uYGXawIDAQAB";
    private static String SIGN_TYPE = "RSA2";

    private static AlipayClient alipayClient;

    static {
        alipayClient = new DefaultAlipayClient(serverUrl, APP_ID, APP_PRIVATE_KEY, FORMAT_JSON, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
    }

    /**
     * @param body           商品描述
     * @param subject        订单名称
     * @param outTradeNo     订单外部编号
     * @param timeOutExpress 超时时长
     * @param amount         金额
     * @param productCode    商品编码
     * @param notifyUrl      支付宝服务器向商户服务器通知支付结果的网址
     * @return
     */
    public static AlipayOrderResult createOrderInfo(String body, String subject, String outTradeNo, String timeOutExpress,
                                                                          String amount, String productCode, String notifyUrl) {

        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", outTradeNo);
        bizContent.put("total_amount", amount);
        bizContent.put("subject", subject);
        bizContent.put("product_code", "QUICK_MSECURITY_PAY");

        request.setBizContent(bizContent.toString());
        request.setNotifyUrl(notifyUrl);
        request.setReturnUrl(notifyUrl);

        AlipayOrderResult result = new AlipayOrderResult();

        try {
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);

            result.orderInfo = response.getBody();

            LogUtil.e("支付宝返回参数", result.orderInfo);

            result.success = true;
        } catch (AlipayApiException e) {
            e.printStackTrace();
            result.success = false;
            result.exception = e;
        }

        return result;
    }

    public static boolean rsaCheckV1(Map<String, String> params) {
        try {
            return AlipaySignature.rsaCheckV1(params, ALIPAY_PUBLIC_KEY, CHARSET, SIGN_TYPE);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean rsaCheck(String content, String sign) {
        try {
            return AlipaySignature.rsa256CheckContent(content, sign, ALIPAY_PUBLIC_KEY, CHARSET);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
